//
//  RegistrationBaseCell.swift
//  PorscheRegistration
//
//  Created by Jinzhu Liu on 2023/11/16.
//

import UIKit

public class RegistrationBaseCell: NiblessTableViewCell, ViewModelUpdatableCell {

    public weak var viewModel: AnyObject?
    public var titleLabel: UILabel
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        self.titleLabel = UILabel()
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func setupUI() {
        self.contentView.addSubview(titleLabel)
        
        titleLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        titleLabel.setContentHuggingPriority(.required, for: .horizontal)
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            titleLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 50.0),
            titleLabel.leadingAnchor.constraint(equalTo: self.contentView.safeAreaLayoutGuide.leadingAnchor, constant: 16.0),
            titleLabel.topAnchor.constraint(equalTo: self.contentView.safeAreaLayoutGuide.topAnchor),
            titleLabel.bottomAnchor.constraint(equalTo: self.contentView.safeAreaLayoutGuide.bottomAnchor)
        ])
    }
    
    public func update(with viewModel: AnyObject?) {
        if let viewModel = viewModel {
            self.viewModel = viewModel
            if let textViewModel = viewModel as? RegistrationTitleCellViewModelOutputType {
                titleLabel.text = textViewModel.title
            }
        }
    }
}

